<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Landing extends Backend_Controller {

	protected $_title = 'SIUL';

	public function __construct() {
		parent::__construct();
		if(parent::get_actor_login()!="teacher")
			redirect('login/index/1');
		$this->load->model('teachings_model', 'teachings', true);
		$this->load->model('school_years_model', 'school_years', true);
		$this->load->model('subjects_model', 'subjects', true);
	}
		
	public function index() {
		
		$rs = $this->school_years->get_active_school_year();
		$subjects = $this->teachings->lists($rs[0]['id'], $_SESSION['person_id']);
				
		$data = array(
			'mainview' => 'index',
			'subject_total' => count($subjects),
			'subjects' => $subjects
		) + $this->_get_header_data();
		
		if(count($subjects) <= 0){
			$this->load->view('teacher/template',$data);
		}
		else if(count($subjects) ==1){
			$this->go( base64_encode($subjects[0]['subject_id']) );
		}
		else
			$this->load->view('teacher/choose_subject',$data);
	}
	
	public function go($subject_id) {
		$_SESSION['subject_id'] = base64_decode($subject_id);
		$_SESSION['subject_name'] = $this->subjects->get_name($_SESSION['subject_id']);
		
		$rs = $this->school_years->get_active_school_year();
		$subjects = $this->teachings->lists($rs[0]['id'], $_SESSION['person_id']);
		
		$data = array(
			'mainview' => 'index',
			'subject_total' => count($subjects),
			'subjects' => $subjects
		) + $this->_get_header_data();
		$this->load->view('teacher/template',$data);
	}	
}
